package com.tangkeyon.controller.manage;

import com.tangkeyon.common.Const;
import com.tangkeyon.common.ResponseCode;
import com.tangkeyon.pojo.Category;
import com.tangkeyon.pojo.User;
import com.tangkeyon.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/5/27 20:06
 * @Version 1.0
 */
@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {

    @Autowired
    private ICategoryService iCategoryService;
    /**
     * @Description: 在管理员登录的情况下允许添加新的类别，若未选择parentId就默认设为0
     * @param session
     * @param categoryName
     * @param parentId
     * @return
     */
    @RequestMapping("/add_category.do")
    @ResponseBody
    public ResponseCode addCategory(HttpSession session,String categoryName,
                                             @RequestParam(value="parentId",defaultValue = "0") Integer parentId){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser !=null){
            if(currentUser.getRole()==Const.Role.ROLE_ADMIN){
            return iCategoryService.addCategory(categoryName,parentId);
            }
            return  ResponseCode.createByErrorMessage("非管理员，没有操作权限");
        }
        return ResponseCode.createByErrorMessage("未登录，请登录后再执行操作");
    }

    /**
     * @Description: 根据类别的id修改类别的名称
     * @param session
     * @param newName
     * @param id
     * @return
     */
    @RequestMapping("/update_category.do")
    @ResponseBody
    public ResponseCode  updateCategory(HttpSession session,@RequestParam(value = "categoryName")String newName,@RequestParam(defaultValue = "0") Integer id){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser !=null){
            if(currentUser.getRole()==Const.Role.ROLE_ADMIN){
                return iCategoryService.updateCategory(newName,id);
            }
            return  ResponseCode.createByErrorMessage("非管理员，没有操作权限");
        }
        return ResponseCode.createByErrorMessage("未登录，请登录后再执行操作");
    }

    /**
     * @Description: 查询categoryId对应节点下的二级子节点
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping("/select_parallelchildcategory.do")
    @ResponseBody
    public ResponseCode<List<Category>> selectParallelChildCategory(HttpSession session,Integer categoryId){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser !=null){
            if(currentUser.getRole()==Const.Role.ROLE_ADMIN){
                return iCategoryService.selectParallelChildCategory(categoryId);
            }
            return  ResponseCode.createByErrorMessage("非管理员，没有操作权限");
        }
        return ResponseCode.createByErrorMessage("未登录，请登录后再执行操作");
    }

    /**
     * @Description: 查询categoryId对应的节点下的无限层级子节点
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping("/select_alllevelchild_category.do")
    @ResponseBody
    public ResponseCode<List<Integer>> selectAllLevelChildCategory(HttpSession session, Integer categoryId){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser !=null){
            if(currentUser.getRole()==Const.Role.ROLE_ADMIN){
                return iCategoryService.selectAllLevelChildCategory(categoryId);
            }
            return  ResponseCode.createByErrorMessage("非管理员，没有操作权限");
        }
        return ResponseCode.createByErrorMessage("未登录，请登录后再执行操作");
        }
    }















